ActiveReports 12
Using Script in a Section Report (XML-based)

In a section report (xml-based), a user can connect to a data source, access report controls and sections and set their properties using scripting. Go through the sections below to learn using the scripts in a xml based section report.

To use a script to create a data connection

Go to the Script tab and add a scripting code like the following:

To write the script in Visual Basic.NET

Visual Basic.NET script. Paste in the script editor window.
Copy Code
Private Shared m_cnn As System.Data.OleDb.OleDbConnection

Public Sub ActiveReport_ReportStart()
  'Set up a data connection for the report
  Dim connString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=[User Documents folder]\GrapeCity Samples\ActiveReports 12\Data\NWIND.mdb"
  Dim sqlString As String = "SELECT * FROM products"
     
  m_cnn = new System.Data.OleDb.OleDbConnection(connString)
  Dim m_Cmd As System.Data.OleDb.OleDbCommand = new System.Data.OleDb.OleDbCommand(sqlString, m_cnn)
   
  If m_cnn.State = System.Data.ConnectionState.Closed Then
     m_cnn.Open
  End If
  rpt.DataSource = m_Cmd.ExecuteReader
End Sub

Public Sub ActiveReport_ReportEnd()
  'Close the data reader and connection
  m_cnn.Close
End Sub

To write the script in C#

C# script. Paste in the script editor window.
Copy Code
private static System.Data.OleDb.OleDbConnection m_cnn;

public void ActiveReport_ReportStart()
{
    //Set up a data connection for the report
    string m_cnnString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=[User Documents folder]\GrapeCity Samples\ActiveReports 12\Data\NWIND.mdb";
    string sqlString = "SELECT * FROM products";
    m_cnn = new System.Data.OleDb.OleDbConnection(m_cnnString);
    System.Data.OleDb.OleDbCommand m_Cmd = new System.Data.OleDb.OleDbCommand(sqlString, m_cnn);
 
    if(m_cnn.State == System.Data.ConnectionState.Closed)
    {
        m_cnn.Open();
    }
    rpt.DataSource = m_Cmd.ExecuteReader();
}

public void ActiveReport_ReportEnd()
{
    //Close the data reader and connection
    m_cnn.Close();
}
Warning: Do not access the Fields collection outside the DataInitialize and FetchData events. Accessing the Fields collection outside of these events is not supported, and has unpredictable results.

 

To add scripting to apply alternate colors in the detail section

Go to the Script tab and add a scripting code like the following:

To write the script in Visual Basic.NET

Visual Basic.NET script. Paste in the script editor window.
Copy Code
Dim b as boolean = true

Sub Detail1_Format
 if b then
  Me.Detail1.BackColor = Color.AliceBlue
  b= false
  else
  me.Detail1.BackColor = Color.Cyan
  b = true
 End If
End Sub

To write the script in C#

C# script. Paste in the script editor window.
Copy Code
bool color = true;
public void Detail1_Format()
{
  if(color)
  {
      this.Detail1.BackColor = System.Drawing.Color.AliceBlue;
      color = false;
  }
  else
  {
      this.Detail1.BackColor = System.Drawing.Color.Cyan;
      color = true;
  }
}